package com.lineage.server.timecontroller.pc;

import com.lineage.server.datatables.lock.CharSkillReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.model.Instance.L1SummonInstance;
import com.lineage.server.model.skill.L1SkillUse;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.PcOtherThreadPool;
import com.lineage.server.world.World;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Pc_Ai_Hp extends TimerTask {
  private static final Log _log = LogFactory.getLog(Pc_Ai_Hp.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 2000;
    this._timer = PcOtherThreadPool.get().scheduleAtFixedRate(this, 2000L, 2000L);
  }
  
  public void run() {
    try {
      Collection<L1PcInstance> all = World.get().getAllPlayers();
      if (all.isEmpty())
        return; 
      Iterator<L1PcInstance> iter = all.iterator();
      while (iter.hasNext()) {
        L1PcInstance tgpc = iter.next();
        if (check(tgpc)) {
          if (tgpc.isActived()) {
            if (tgpc.getchecksummid() && !tgpc.hasSkillEffect(9981)) {
              tgpc.setSkillEffect(9981, 1800000);
              L1SkillUse skillUse = new L1SkillUse();
              skillUse.handleCommands(tgpc, 51, tgpc.getId(), tgpc.getX(), tgpc.getY(), 0, 4);
            } 
            if (tgpc.getchecksummid()) {
              Iterator<L1NpcInstance> iterator = tgpc.getPetList().values().iterator();
              while (iterator.hasNext()) {
                L1NpcInstance petNpc = iterator.next();
                if (petNpc instanceof L1SummonInstance) {
                  L1SummonInstance summon = (L1SummonInstance)petNpc;
                  if (!tgpc.getchecksummidhp() || summon.isDead() || petNpc == null)
                    continue; 
                  if (tgpc.getCurrentMp() > tgpc.getsummon_skillidmp() && 
                    summon.getCurrentHp() < summon.getMaxHp() / 2) {
                    L1SkillUse l1SkillUse = new L1SkillUse();
                    l1SkillUse.handleCommands(tgpc, tgpc.getsummon_skillid(), summon.getId(), 
                        summon.getX(), summon.getY(), 0, 2);
                    tgpc.setCurrentMp(tgpc.getCurrentMp() - tgpc.getsummon_skillidmp());
                  } 
                  if (!CharSkillReading.get().spellCheck(tgpc.getId(), 43) || 
                    tgpc.getCurrentMp() <= 40 || summon.hasSkillEffect(43))
                    continue; 
                  L1SkillUse skillUse2 = new L1SkillUse();
                  skillUse2.handleCommands(tgpc, 43, summon.getId(), summon.getX(), summon.getY(), 0, 
                      2);
                  tgpc.setCurrentMp(tgpc.getCurrentMp() - 40);
                } 
              } 
            } 
            Iterator<L1NpcInstance> iterator2 = tgpc.getPetList().values().iterator();
            while (iterator2.hasNext()) {
              L1NpcInstance petNpc = iterator2.next();
              if (petNpc instanceof L1PetInstance) {
                L1PetInstance pet = (L1PetInstance)petNpc;
                if (!tgpc.getchecksummidhp() || pet.isDead() || petNpc == null)
                  continue; 
                if (tgpc.getCurrentMp() > tgpc.getsummon_skillidmp() && 
                  pet.getCurrentHp() < pet.getMaxHp() / 2) {
                  L1SkillUse l1SkillUse = new L1SkillUse();
                  l1SkillUse.handleCommands(tgpc, tgpc.getsummon_skillid(), pet.getId(), pet.getX(), 
                      pet.getY(), 0, 2);
                  tgpc.setCurrentMp(tgpc.getCurrentMp() - tgpc.getsummon_skillidmp());
                } 
                if (!CharSkillReading.get().spellCheck(tgpc.getId(), 43) || tgpc.getCurrentMp() <= 40 || 
                  pet.hasSkillEffect(43))
                  continue; 
                L1SkillUse skillUse2 = new L1SkillUse();
                skillUse2.handleCommands(tgpc, 43, pet.getId(), pet.getX(), pet.getY(), 0, 2);
                tgpc.setCurrentMp(tgpc.getCurrentMp() - 40);
              } 
            } 
          } 
          L1ItemInstance item = tgpc.getInventory().findItemId(tgpc.getitemitemid());
          L1ItemInstance item2 = tgpc.getInventory().findItemId(tgpc.getitemitemid1());
          L1ItemInstance item3 = tgpc.getInventory().findItemId(tgpc.getitemitemid2());
          L1ItemInstance item4 = tgpc.getInventory().findItemId(40733);
          L1ItemInstance item5 = tgpc.getInventory().findItemId(49138);
          if (!tgpc.isActived())
            continue; 
          if (item == null && tgpc.getitempotion() > 0 && tgpc.getpotioncount() > 0 && 
            tgpc.getInventory().checkItem(40308, (tgpc.getpotioncount() * tgpc.getitemadena()))) {
            tgpc.getInventory().storeItem(tgpc.getitemitemid(), tgpc.getpotioncount());
            tgpc.getInventory().consumeItem(40308, (tgpc.getpotioncount() * tgpc.getitemadena()));
            tgpc.sendPackets((ServerBasePacket)new S_SystemMessage("系统自动补充药水完毕"));
          } 
          if (item2 == null && tgpc.getitempotion1() > 0 && tgpc.getpotioncount1() > 0 && 
            tgpc.getInventory().checkItem(40308, (tgpc.getpotioncount1() * tgpc.getitemadena1()))) {
            tgpc.getInventory().storeItem(tgpc.getitemitemid1(), tgpc.getpotioncount1());
            tgpc.getInventory().consumeItem(40308, (tgpc.getpotioncount1() * tgpc.getitemadena1()));
            tgpc.sendPackets((ServerBasePacket)new S_SystemMessage("系统自动补充药水完毕"));
          } 
          if (item3 != null || tgpc.getitempotion2() <= 0 || tgpc.getpotioncount2() <= 0 || 
            !tgpc.getInventory().checkItem(40308, (tgpc.getpotioncount2() * tgpc.getitemadena2())))
            continue; 
          tgpc.getInventory().storeItem(tgpc.getitemitemid2(), tgpc.getpotioncount2());
          tgpc.getInventory().consumeItem(40308, (tgpc.getpotioncount2() * tgpc.getitemadena2()));
          tgpc.sendPackets((ServerBasePacket)new S_SystemMessage("系统自动补充箭矢完毕"));
        } 
      } 
    } catch (Exception e) {
      _log.error("自动购水处理时间轴异常重启", e);
      PcOtherThreadPool.get().cancel(this._timer, false);
      Pc_Ai_Hp pc_Ai_Hp = new Pc_Ai_Hp();
      pc_Ai_Hp.start();
    } 
  }
  
  private static boolean check(L1PcInstance tgpc) {
    try {
      if (tgpc == null)
        return false; 
      if (tgpc.getOnlineStatus() == 0)
        return false; 
      if (tgpc.getNetConnection() == null)
        return false; 
      if (tgpc.isTeleport())
        return false; 
      if (tgpc.isDead())
        return false; 
      if (tgpc.getCurrentHp() <= 0)
        return false; 
      if (tgpc.isParalyzed() || tgpc.hasSkillEffect(33) || tgpc.hasSkillEffect(4000) || tgpc.hasSkillEffect(192) || 
        tgpc.hasSkillEffect(50) || tgpc.hasSkillEffect(66) || tgpc.hasSkillEffect(87) || 
        tgpc.hasSkillEffect(4017) || tgpc.hasSkillEffect(192))
        return false; 
    } catch (Exception e) {
      return false;
    } 
    return true;
  }
}
